//
//  FYHomeViewController.swift
//  Mircore
//
//  Created by apple on 16/8/8.
//  Copyright © 2016年 飞羽. All rights reserved.
//

import UIKit

//重用标记

private  let HomeTableViewCellIdentifier  = "HomeTableViewCellIdentifier"


//首页
class FYHomeViewController: FYVisitorTableViewController {

    //使用懒加载方式创建所需要的ViewModel
    
    private  lazy var  statusListViewModel: FYStatusListViewModel  = FYStatusListViewModel()
    
    
    //风火轮
    
    private  lazy  var  pullUpView: UIActivityIndicatorView  = {
    
        let  view  = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.WhiteLarge)
    
          view.color  = UIColor.redColor()
        
          return  view
    }()
    
    
//    //系统的下拉刷新控件
//    
//    private lazy  var  refreshCtr: UIRefreshControl = {
//    
//     let  ctr = UIRefreshControl()
//        
//        ctr.addTarget(self, action: "pullDownRefresh", forControlEvents: .ValueChanged)
//    
//        
//        return ctr
//    
//    }()
    
    //自定义下拉刷新控件
    
    private  lazy  var  pullDownView: FYRefreshControl = {
    
    let  ctr  = FYRefreshControl()
        
        ctr.addTarget(self, action: #selector(FYHomeViewController.pullDownRefresh), forControlEvents: .ValueChanged)
    
      return  ctr
    }()
    
    
    //tip图片label
    
    private lazy  var  tipLabel: UILabel  = {
    
      let  label  = UILabel(fontSize: 12, textColor: UIColor.whiteColor())
        
        label.backgroundColor = UIColor.orangeColor()
        
        label.text = "没有加载到微博数据"
        
        label.textAlignment = .Center
        
        //默认隐藏
        
        label.hidden = true
        
        return  label
     
    }()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        if isLogin  {
            
            setupUI()
            
            //登录成功
            
            loadData()
        
        
        }else  {
        
          visitorView?.updateVisitorViewInfo(nil, imageName: nil)
          }
    }

    
    //添加控件设置约束
    
    private  func setupUI() {
    
        //设置tableview相关操作
        
        setupTableView()
        
        
        //判断导航控制器是否存在  存在插入tiplabel
        
        if  let  nav  = self.navigationController {
        
          tipLabel.frame  = CGRect(x: 0, y: CGRectGetMaxY(nav.navigationBar.frame) - 30, width: nav.navigationBar.width, height: 30)
        
          nav.view.insertSubview(tipLabel, belowSubview: nav.navigationBar)
        }
    
    
    
    }
      //设置tableView相关操作
    
    private  func  setupTableView()  {
    
      tableView.registerClass(FYStatusTableViewCell.self, forCellReuseIdentifier: HomeTableViewCellIdentifier)
        
        //设置行高
        
        //自动计算行高
        
        tableView.rowHeight  = UITableViewAutomaticDimension
        
        //预估行高
        
        tableView.estimatedRowHeight  = 200
        
        //去掉分割线
        
        tableView.separatorStyle  = .None
        
        //添加上拉刷新控件
        
        tableView.tableFooterView  = pullUpView
        
        
        //使用自定义下拉刷新控件
        
        tableView.addSubview(pullDownView)
    
    }
    
    @objc  private  func  pullDownRefresh() {
    
    
     loadData()
    
    }
    
    //结束刷新的方法
    
    private  func endRefreshing() {
    
    //停止菊花转
        
        pullUpView.stopAnimating()
        
        //结束系统下拉刷新的状态
       
//        refreshControl?.endRefreshing()
        
        //结束自定义下拉刷新的状态
        
        pullDownView.endRefreshing()
    
    }
    
    
    //根据服务端返回的信息显示tip文字
    
    private  func   startAnimation(message: String)  {
    
    //判断视图如果显示表示正在执行动画  让其直接返回不执行下面的代码
        
        if  tipLabel.hidden  == false  {
        
        return
        
        }
    
        //设置显示文字
        
        tipLabel.text = message
        
        //显示tiplabel
        
        tipLabel.hidden =  false
        
    UIView.animateWithDuration(1, animations: { 
        
        self.tipLabel.transform  = CGAffineTransformMakeTranslation(0, self.tipLabel.height)
        
        }) { (_) in
            
            UIView.animateWithDuration(1, animations: { 
                
                self.tipLabel.transform = CGAffineTransformIdentity
                
                }, completion: { (_) in
                    
                    //隐藏tiplabel
                    
                    self.tipLabel.hidden  = true
            })
            
        }
        
    }
    
    //加载微博数据
    
    private  func  loadData()  {
    
        statusListViewModel.loadData (pullUpView.isAnimating()){ (isSuccesss,message) in
            
            //开启tip动画
            
            if  !self.pullUpView.isAnimating()  {
            
            self.startAnimation(message)
            
            }
            
            self.endRefreshing()
            if  isSuccesss {
            
             //数据请求重写刷新数据
                
                self.tableView.reloadData()
            }else {
            
            print("数据请求异常")
            }
        }
        
       }
    
    
}

//MARK:-  UITableViewDataSource  数据源代理
    extension  FYHomeViewController  {
    
        override  func  tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            
            
            return  statusListViewModel.statusList.count
        }
    
        
        override  func  tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
            
            let  cell = tableView.dequeueReusableCellWithIdentifier(HomeTableViewCellIdentifier, forIndexPath: indexPath) as! FYStatusTableViewCell
            
           //给cell绑定数据
            
            cell.statusViewModel  = statusListViewModel.statusList[indexPath.row]
            
            
            //取消选中颜色
            
            cell.selectionStyle  = .None
            return cell
        }
        
        override  func  tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
            
            
            if  indexPath.row  == statusListViewModel.statusList.count - 1 && !pullUpView.isAnimating() {
            
            
             //开启风火轮
                
                pullUpView.startAnimating()
                
                //加载更多数据
                
                loadData()
                
            }
            
        }
    
    }



